<template>
  <BasicModal :footer="null" title="修改密码" v-bind="$attrs" :class="prefixCls" @register="register">
    <BasicForm @register="registerForm" />

    <div :class="`${prefixCls}__footer`">
      <a-button type="primary" block class="mt-2" @click="handleLock"> 确认修改 </a-button>
    </div>
  </BasicModal>
</template>
<script lang="ts" setup>
import { computed } from 'vue';
import { useI18n } from '@/hooks/web/useI18n';
import { useDesign } from '@/hooks/web/useDesign';
import { BasicModal, useModalInner } from '@/components/Modal';
import { BasicForm, useForm } from '@/components/Form';

import { useUserStore } from '@/store/modules/user';
import { useLockStore } from '@/store/modules/lock';
import headerImg from '@/assets/images/header.jpg';
import { useMessage } from '@/hooks/web/useMessage';
import { editPassword } from '@/api/system/user';

import aes from '@/utils/aes'

defineOptions({ name: 'LockModal' });

const { t } = useI18n();
const { prefixCls } = useDesign('header-lock-modal');
const userStore = useUserStore();
const lockStore = useLockStore();

const getRealName = computed(() => userStore.getUserInfo?.realName);
const [register, { closeModal }] = useModalInner();

const { createMessage: msg } = useMessage();

const [registerForm, { validate, resetFields }] = useForm({
  showActionButtonGroup: false,
  schemas: [
    {
      field: 'oldPassword',
      label: '原密码',
      component: 'StrengthMeter',
      required: true,
      colProps: { lg: 24, md: 24 },
    },
    {
      field: 'newPassword',
      label: '新密码',
      component: 'StrengthMeter',
      required: true,
      colProps: { lg: 24, md: 24 },
    },
  ],
});

const handleLock = async () => {
  const values = await validate();

  let request = {
    newPassword: aes.encrypt(values.newPassword),
    oldPassword: aes.encrypt(values.oldPassword),
  }

  const res = await editPassword(request);

  closeModal();

  if (res.isSucceed) {
    msg.success('操作成功');
    closeModal();
    emit('success');
  }

  await resetFields();
};

const avatar = computed(() => {
  const { avatar } = userStore.getUserInfo;
  return avatar || headerImg;
});
</script>
<style lang="less">
@prefix-cls: ~'@{namespace}-header-lock-modal';

.@{prefix-cls} {
  &__entry {
    position: relative;
    //height: 240px;
    padding: 130px 30px 30px;
    border-radius: 10px;
  }

  &__header {
    position: absolute;
    top: 0;
    left: calc(50% - 45px);
    width: auto;
    text-align: center;

    &-img {
      width: 70px;
      border-radius: 50%;
    }

    &-name {
      margin-top: 5px;
    }
  }

  &__footer {
    text-align: center;
  }
}
</style>
